@echo off
chcp 936 >nul
echo ================================
echo    Simple Microservices Starter
echo ================================
echo.

:: Set project root
set PROJECT_ROOT=F:\shopping
echo Project Root: %PROJECT_ROOT%

:: Check environments
echo Checking environments...
java -version >nul 2>&1
if errorlevel 1 (
    echo ERROR: Java not found
    pause
    exit /b 1
)

mvn -version >nul 2>&1
if errorlevel 1 (
    echo ERROR: Maven not found
    pause
    exit /b 1
)

echo Environments OK
echo.
echo Starting compilation...
echo.

:: Compile modules
echo [1/6] Compiling common-module...
cd /d "%PROJECT_ROOT%\common-module"
call mvn clean install -DskipTests -q
if errorlevel 1 goto :error

echo [2/6] Compiling auth-service...
cd /d "%PROJECT_ROOT%\auth-service"
call mvn clean package -DskipTests -q
if errorlevel 1 goto :error

echo [3/6] Compiling user-service...
cd /d "%PROJECT_ROOT%\user-service"
call mvn clean package -DskipTests -q
if errorlevel 1 goto :error

echo [4/6] Compiling product-service...
cd /d "%PROJECT_ROOT%\product-service"
call mvn clean package -DskipTests -q
if errorlevel 1 goto :error

echo [5/6] Compiling order-service...
cd /d "%PROJECT_ROOT%\order-service"
call mvn clean package -DskipTests -q
if errorlevel 1 goto :error

echo [6/6] Compiling gateway-service...
cd /d "%PROJECT_ROOT%\gateway-service"
call mvn clean package -DskipTests -q
if errorlevel 1 goto :error

echo.
echo All modules compiled successfully!
echo.
echo Starting services...
echo.

:: Start services
echo Starting auth-service (port 8084)...
start "auth-service" cmd /k "cd /d "%PROJECT_ROOT%\auth-service" && java -jar target\*.jar"
timeout /t 5 /nobreak >nul

echo Starting user-service (port 8081)...
start "user-service" cmd /k "cd /d "%PROJECT_ROOT%\user-service" && java -jar target\*.jar"
timeout /t 5 /nobreak >nul

echo Starting product-service (port 8082)...
start "product-service" cmd /k "cd /d "%PROJECT_ROOT%\product-service" && java -jar target\*.jar"
timeout /t 5 /nobreak >nul

echo Starting order-service (port 8083)...
start "order-service" cmd /k "cd /d "%PROJECT_ROOT%\order-service" && java -jar target\*.jar"
timeout /t 5 /nobreak >nul

echo Starting gateway-service (port 8080)...
start "gateway-service" cmd /k "cd /d "%PROJECT_ROOT%\gateway-service" && java -jar target\*.jar"

echo.
echo ================================
echo    Services started!
echo ================================
echo.
echo Press any key to exit...
pause >nul
exit /b 0

:error
echo ERROR: Compilation failed
echo Check the error messages above
pause
exit /b 1